#include "PDTConst.h"

#include "PDTCodeGraph.h"
#include "PDTBoolConst.h"
#include "PDTNumConst.h"

PDTConst::PDTConst(){
}

//static members
map<long long,PDTConst *> PDTConst::intConsts;
map<double,PDTConst *> PDTConst::doubleConsts;
PDTBoolConst * PDTConst::PDTTrue;
PDTBoolConst * PDTConst::PDTFalse;

PDTConst * 
PDTConst::makeConst(PDTCodeGraph& cg, int i) {
    map<long long,PDTConst *>::iterator it = intConsts.find(i);
    if (it == intConsts.end()) {
        PDTConst * iConst = new PDTNumConstInt(cg, i);
        intConsts[i] = iConst;
        return iConst;
    }
    return it->second;
}

PDTConst * 
PDTConst::makeConst(PDTCodeGraph& cg, long long i) {
    map<long long,PDTConst *>::iterator it = intConsts.find(i);
    if (it == intConsts.end()) {
        PDTConst * iConst = new PDTNumConstInt(cg, i);
        intConsts[i] = iConst;
        return iConst;
    }
    return it->second;
}

PDTConst * 
PDTConst::makeConst(PDTCodeGraph & cg, bool b) {
    if (PDTTrue == NULL) {
        PDTTrue = new PDTBoolConst(cg, true);
        PDTFalse = new PDTBoolConst(cg, false);
    }
    return b? PDTTrue : PDTFalse;
}

PDTConst * 
PDTConst::makeConst(PDTCodeGraph &cg, double v) {
    map<double,PDTConst *>::iterator it = doubleConsts.find(v);
    if (it == doubleConsts.end()) {
        PDTConst * vConst = new PDTNumConstDbl(cg, v);
        intConsts[v] = vConst;
        return vConst;
    }
    return it->second;
}
